package com.juben.svr.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.juben.svr.domain.ChangePassWordRequest;
import com.juben.svr.domain.Email_validation;
import com.juben.svr.domain.Enum.EmailType;
import com.juben.svr.domain.User;
import com.juben.svr.domain.role;
import com.juben.svr.mapper.Emailmapper;
import com.juben.svr.mapper.usermapper;
import com.juben.svr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

import static com.juben.svr.domain.Enum.EmailType.注册邮箱验证;

//import static com.juben.svr.domain.Enum.EmailType.注册邮箱验证;

@Service
public class UserServiceimpl implements UserService {

    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    private usermapper usermapper;  // 自动注入

    @Autowired
    private Emailmapper emailmapper;




    @Override
    public User getUserById(Integer uid) {
        return usermapper.getUserById(uid);
    }

    public  Boolean userlogin(User user){
        Boolean islogin = false;
        String account = user.getAccount();
        String phone = user.getPhone();
        System.out.println("account:"+account);
        String password = user.getUpassword();
        System.out.println("password:"+password);
        System.out.println("phone:"+phone);
        if ( account == null){
            account = "0000";
            System.out.println("根据电话登录");
            if (usermapper.getUserByPhone(phone) != null) {
                if (password.equals(usermapper.getUserByPhone(phone).getUpassword())) {
                    System.out.println("phone is ok");
                    islogin = true;
                }
            }
        }

        if (phone == null){
            phone = "0000";
            System.out.println("根据账号登录");
            if (usermapper.getUserByAccount(account) != null) {
                if (user.getUpassword().equals(usermapper.getUserByAccount(account).getUpassword())) {
                    System.out.println("account is ok");
                    System.out.println("user:" + usermapper.getUserByAccount(account));
                    islogin = true;
                }
            }
        }
        System.out.println("islogin:"+islogin);
        return islogin;
    }

    public User getUserByAccount(String account){
        return usermapper.getUserByAccount(account);
    }

    public User getUserByPhone(String phone){
        return usermapper.getUserByPhone(phone);
    }

    public Boolean updateUserInfo(User user){
        System.out.println("updateUserInfo");
        System.out.println("user:"+user);
        return usermapper.updateUserInfo(user);
    }

//    发送注册验证码邮件实现类
    @Override
    public User RegisterEmail(String email) {
        System.out.println("邮件发送服务：");
        System.out.println("email:"+email);
        SimpleMailMessage message = new SimpleMailMessage();
        DateTime now = DateUtil.date(); // 获取当前时间
        message.setSentDate(now);
        message.setSubject("【幕起云端】注册邮箱验证");  // 邮件主题
       String code = RandomUtil.randomNumbers(6);
        message.setText("您的账号注册邮箱验证码为："+code+",有效期5分钟；请妥善保管，请勿泄露给他人。");
        message.setTo(email);
        message.setFrom("2811097455@qq.com");
        javaMailSender.send(message);

        //将验证码存入数据库

        System.out.println("将验证码存入数据库");
        Email_validation email_validation = new Email_validation();
        email_validation.setCode(code);
        email_validation.setEmail(email);
        email_validation.setEmailType(注册邮箱验证);
        System.out.println("now:"+now);
        //将时间往后偏移5分钟
        Date updatedTime = DateUtil.offsetMinute(now, 5);
        email_validation.setTime(
                DateUtil.format(updatedTime, "yyyy-MM-dd HH:mm:ss")
        );
        System.out.println("email_validation:"+email_validation);
        emailmapper.addEmailValidation(email_validation);

        return null;
    }

    @Override
    public boolean addUser(User user){
        return usermapper.addUser(user);
    }

    //获取角色类型列表
    public List<role> getRoleList(){
        return usermapper.getRoleList();
    }


    //更新密码
    public boolean changePassword(int userId, ChangePassWordRequest  request){
        System.out.println("changePassword");
        System.out.println("userId:"+userId);
        System.out.println("获取到的更新信息:"+request);
        // 获取用户信息
        User user = usermapper.getUserById(userId);
        System.out.println("user:"+user);
        // 更新密码
        user.setUpassword(request.getNewPassword());
        System.out.println("更新过后的用户信息:"+user);
        //修改密码，保存到数据库
        boolean result = usermapper.changePassword(user);
        System.out.println("result:"+result);
        return result;
    }

}

